home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / programr / atre27.exe / ATREE_27 / LFWIN / LFEDIT.C < prev    next >
C/C++ Source or Header  |  1992-08-01  |  14KB  |  468 lines

  1. /*****************************************************************************
  2.  ****                                                                     ****
  3.  **** lfedit.c                                                            ****
  4.  ****                                                                     ****
  5.  **** atree release 2.7 for Windows                                       ****
  6.  **** Adaptive Logic Network (ALN) simulation program.                    ****
  7.  **** Copyright (C) A. Dwelly, R. Manderscheid, M. Thomas, W.W. Armstrong ****
  8.  ****               1991, 1992                                            ****
  9.  ****                                                                     ****                                                  ****
  10.  **** License:                                                            ****
  11.  **** A royalty-free license is granted for the use of this software for  ****
  12.  **** NON_COMMERCIAL PURPOSES ONLY. The software may be copied and/or     ****
  13.  **** modified provided this notice appears in its entirety and unchanged ****
  14.  **** in all derived source programs.  Persons modifying the code are     ****
  15.  **** requested to state the date, the changes made and who made them     ****
  16.  **** in the modification history.                                        ****
  17.  ****                                                                     ****
  18.  **** Patent License:                                                     ****
  19.  **** The use of a digital circuit which transmits a signal indicating    ****
  20.  **** heuristic responsibility is protected by U. S. Patent 3,934,231     ****
  21.  **** and others assigned to Dendronic Decisions Limited of Edmonton,     ****
  22.  **** W. W. Armstrong, President.  A royalty-free license is granted      ****
  23.  **** by the company to use this patent for NON_COMMERCIAL PURPOSES to    ****
  24.  **** adapt logic trees using this program and its modifications.         ****
  25.  ****                                                                     ****
  26.  **** Limited Warranty:                                                   ****
  27.  **** This software is provided "as is" without warranty of any kind,     ****
  28.  **** either expressed or implied, including, but not limited to, the     ****
  29.  **** implied warrantees of merchantability and fitness for a particular  ****
  30.  **** purpose.  The entire risk as to the quality and performance of the  ****
  31.  **** program is with the user.  Neither the authors, nor the             ****
  32.  **** University of Alberta, its officers, agents, servants or employees  ****
  33.  **** shall be liable or responsible in any way for any damage to         ****
  34.  **** property or direct personal or consequential injury of any nature   ****
  35.  **** whatsoever that may be suffered or sustained by any licensee, user  ****
  36.  **** or any other party as a consequence of the use or disposition of    ****
  37.  **** this software.                                                      ****
  38.  **** Modification history:                                               ****
  39.  ****                                                                     ****
  40.  **** 91.05.20 Initial implementation, M. Thomas                          ****
  41.  **** 91.07.17 atree v2.0 for Windows, M. Thomas                          ****
  42.  **** 92.04.27 atree v2.5 for Windows, M. Thomas                          ****
  43.  **** 92.03.07 Release 2.6, Monroe Thomas                                 ****
  44.  **** 92.01.08 Release 2.7, Monroe Thomas                                 ****
  45.  ****                                                                     ****
  46.  *****************************************************************************/
  47.  
  48. #include <windows.h>
  49. #include "lfedit.h"
  50. #include "atree.h"
  51. #define EDITID 1
  52.  
  53. long FAR PASCAL WndProc(HWND, WORD, WORD, LONG);
  54.  
  55. // in lfeditio.c
  56. BOOL ReadFile (HWND, HWND, POFSTRUCT, LPSTR, BOOL) ;
  57. BOOL WriteFile (HWND, HWND, POFSTRUCT, LPSTR, BOOL) ;
  58. BOOL GetOutFile (HWND) ;
  59. BOOL PrintFile (HANDLE, HWND, HWND, LPSTR);
  60.  
  61. // in lf.c
  62. extern BOOL PASCAL lf_main(HWND, LPSTR, LPSTR, HANDLE); /*in lf.c*/
  63. extern BOOL lf_quit;
  64. extern int line_no;
  65.  
  66. // filedlg.c
  67. LPSTR lstrrchr (LPSTR, char);
  68.  
  69. char szAppName []  = "lfEdit";
  70. static char szUntitled [] = "(untitled)";
  71. char szFileNameIn[128] ;
  72. char szFileNameOut[128];
  73.  
  74. BOOL bIsProcessing = FALSE;
  75.  
  76. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  77.                    LPSTR lpszCmdLine, int nCmdShow)
  78.  
  79. {
  80.     HWND hwnd;
  81.     MSG msg;
  82.     HANDLE hAccel;
  83.     WNDCLASS wndclass;
  84.  
  85.     if (!hPrevInstance)
  86.     {
  87.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  88.         wndclass.lpfnWndProc    = (long (FAR PASCAL*)())WndProc;
  89.         wndclass.cbClsExtra     = 0;
  90.         wndclass.cbWndExtra     = 0;
  91.         wndclass.hInstance      = hInstance;
  92.         wndclass.hIcon          = LoadIcon(hInstance, "atreeico");
  93.         wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  94.         wndclass.hbrBackground  = COLOR_WINDOW + 1;
  95.         wndclass.lpszMenuName   = szAppName;
  96.         wndclass.lpszClassName  = szAppName;
  97.  
  98.         RegisterClass(&wndclass);
  99.     }
  100.     else
  101.     {
  102.         MessageBox(NULL, "LF is already running!", "LF", MB_OK);
  103.         return FALSE;
  104.     }
  105.  
  106.     hwnd = CreateWindow(szAppName, NULL,
  107.                                             WS_OVERLAPPEDWINDOW,
  108.                                             CW_USEDEFAULT,
  109.                                             CW_USEDEFAULT,
  110.                                             CW_USEDEFAULT,
  111.                                             CW_USEDEFAULT,
  112.                                             NULL, NULL, hInstance, lpszCmdLine) ;
  113.  
  114.     ShowWindow (hwnd, nCmdShow);
  115.     UpdateWindow (hwnd);
  116.  
  117.     hAccel = LoadAccelerators(hInstance, szAppName);
  118.  
  119.     while (GetMessage (&msg, NULL, 0, 0))
  120.         {
  121.         if (!TranslateAccelerator(hwnd, hAccel, &msg))
  122.             {
  123.             TranslateMessage(&msg);
  124.             DispatchMessage(&msg);
  125.             }
  126.         }
  127.     return msg.wParam;
  128.     }
  129.  
  130. #pragma argsused
  131.  
  132. BOOL FAR PASCAL AboutDlgProc(HWND hDlg, WORD message, WORD wParam, LONG lParam)
  133. {
  134.     switch(message)
  135.     {
  136.         case WM_INITDIALOG:
  137.             return TRUE;
  138.  
  139.         case WM_COMMAND :
  140.             switch (wParam)
  141.             {
  142.                 case IDOK:
  143.                     EndDialog(hDlg, 0);
  144.                     return TRUE;
  145.             }
  146.             break;
  147.     }
  148.     return FALSE;
  149. }
  150.  
  151. void DoCaption (HWND hwnd, LPSTR szFileName)
  152. {
  153.     char szCaption [40];
  154.  
  155.     wsprintf(szCaption, "%s - %s", (LPSTR) szAppName,
  156.               (LPSTR) (szFileName[0] ? szFileName : szUntitled));
  157.  
  158.     SetWindowText(hwnd, szCaption);
  159. }
  160.  
  161. short AskAboutSave (HWND hwnd, LPSTR szFileName)
  162. {
  163.     char szBuffer[80];
  164.     short nReturn;
  165.  
  166.     wsprintf(szBuffer, "Save current changes: %s",
  167.              (LPSTR) (szFileName[0] ? szFileName : szUntitled));
  168.  
  169.     if (IDYES == (nReturn = MessageBox(hwnd, szBuffer, szAppName,
  170.                                 MB_YESNOCANCEL | MB_ICONQUESTION)))
  171.     {
  172.         if (!SendMessage(hwnd, WM_COMMAND, IDM_SAVE, 0L))
  173.             return IDCANCEL;
  174.     }
  175.     return nReturn;
  176. }
  177.  
  178. short AskAboutProcess (HWND hwnd, LPSTR szFileName)
  179. {
  180.     char szBuffer[80];
  181.     short nReturn;
  182.  
  183.     wsprintf(szBuffer, "Are you sure you wish to quit while processing %s?",
  184.                 (LPSTR) szFileName);
  185.  
  186.     nReturn = MessageBox(hwnd, szBuffer, szAppName,
  187.                                 MB_OKCANCEL | MB_ICONQUESTION);
  188.  
  189.     return nReturn;
  190. }
  191.  
  192. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  193. {
  194.     static BOOL     bNeedSave = FALSE;
  195.     static char     szRealFileName[16];
  196.     static FARPROC  lpfnAboutDlgProc;
  197.     static HANDLE   hInst;
  198.     static HWND     hwndEdit;
  199.     static char     szFileName[128];
  200.     LONG            lSelect;
  201.     OFSTRUCT        of;
  202.     WORD            wEnable;
  203.  
  204.     switch(message)
  205.     {
  206.         case WM_CREATE :
  207.  
  208.             hInst = ((LPCREATESTRUCT) lParam) -> hInstance;
  209.             lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  210.  
  211.             hwndEdit = CreateWindow ("edit", NULL,
  212.                     WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
  213.                         WS_BORDER | ES_LEFT | ES_MULTILINE | ES_OEMCONVERT |
  214.                         ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  215.                     0,0,0,0,
  216.                     hwnd, EDITID, hInst, NULL);
  217.  
  218.             line_no = 1;
  219.             SendMessage (hwndEdit, EM_LIMITTEXT, 64000, 0L);
  220.             SendMessage (hwndEdit, WM_SETFONT, GetStockObject(SYSTEM_FIXED_FONT), TRUE);
  221.  
  222.             if (lstrlen (((LPCREATESTRUCT) lParam) -> lpCreateParams))
  223.             {
  224.                 OpenFile (((LPCREATESTRUCT) lParam) -> lpCreateParams, &of,    OF_PARSE);
  225.  
  226.                 lstrcpy  (szFileName, AnsiNext (lstrrchr (of.szPathName, '\\')));
  227.  
  228.                 if (ReadFile (hwnd, hwndEdit, &of, (LPSTR)szFileName, FALSE))
  229.         {
  230.                     lstrcpy(szRealFileName, szFileName);
  231.                 }
  232.             }
  233.  
  234.             DoCaption (hwnd, szRealFileName);
  235.             return 0;
  236.  
  237.         case WM_SETFOCUS:
  238.             SetFocus(hwndEdit);
  239.             return 0;
  240.  
  241.         case WM_SIZE:
  242.             MoveWindow (hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  243.             return 0;
  244.  
  245.         case WM_INITMENUPOPUP:
  246.             if (lParam == 1)        /* Edit Menu */
  247.             {
  248.                 EnableMenuItem(wParam, IDM_UNDO, SendMessage(hwndEdit, EM_CANUNDO, 0, 0L) ?
  249.                                            MF_ENABLED : MF_GRAYED);
  250.  
  251.                 EnableMenuItem(wParam, IDM_PASTE,    IsClipboardFormatAvailable(CF_TEXT) ?
  252.                                                MF_ENABLED : MF_GRAYED);
  253.  
  254.                 lSelect = SendMessage(hwndEdit, EM_GETSEL, 0, 0L);
  255.  
  256.                 if (HIWORD(lSelect) == LOWORD(lSelect))
  257.         {
  258.                     wEnable = MF_GRAYED;
  259.         }
  260.                 else
  261.         {
  262.                     wEnable = MF_ENABLED;
  263.         }
  264.  
  265.                 EnableMenuItem (wParam, IDM_CUT,   wEnable);
  266.                 EnableMenuItem (wParam, IDM_COPY,  wEnable);
  267.                 EnableMenuItem (wParam, IDM_CLEAR, wEnable);
  268.             }
  269.  
  270.             else if (lParam == 0)   /*File menu */
  271.             {
  272.                 if (szRealFileName[0])
  273.         {
  274.                     wEnable = MF_ENABLED;
  275.         }
  276.                 else
  277.         {
  278.                     wEnable = MF_GRAYED;
  279.         }
  280.  
  281.                 EnableMenuItem (wParam, IDM_RUN,   wEnable);
  282.                 EnableMenuItem (wParam, IDM_PRINT, wEnable);
  283.             }
  284.             return 0;
  285.  
  286.         case WM_COMMAND:
  287.             if (LOWORD(lParam) && wParam == EDITID)
  288.             {
  289.                 switch (HIWORD(lParam))
  290.                 {
  291.                     case EN_UPDATE :
  292.                         bNeedSave = TRUE;
  293.                         return 0;
  294.  
  295.                     case EN_ERRSPACE :
  296.                         MessageBox(hwnd, "Editor is out of space.",    szAppName,
  297.                                              MB_OK | MB_ICONSTOP);
  298.                         return 0;
  299.                 }
  300.                 break;
  301.             }
  302.  
  303.             switch (wParam)
  304.             {
  305.                 case IDM_HELP:
  306.                     WinHelp(hwnd, "atree.hlp", HELP_CONTEXT, 40);
  307.                     break;
  308.  
  309.                 case IDM_NEW :
  310.                     if(bNeedSave && IDCANCEL == AskAboutSave(hwnd, szRealFileName))
  311.           {
  312.                         return 0;
  313.           }
  314.                     SetWindowText(hwndEdit, "\0");
  315.                     szRealFileName[0] = '\0';
  316.                     DoCaption (hwnd, szRealFileName);
  317.                     bNeedSave = FALSE;
  318.                     line_no = 1;
  319.                     return(0);
  320.  
  321.                 case IDM_OPEN :
  322.                     if (bNeedSave && IDCANCEL == AskAboutSave (hwnd, szRealFileName))
  323.           {
  324.                         return 0;
  325.           }
  326.  
  327.                     if (ReadFile (hwnd, hwndEdit, &of, (LPSTR)szFileName, TRUE))
  328.                     {
  329.                         lstrcpy (szRealFileName, szFileName);
  330.                         DoCaption (hwnd, szRealFileName);
  331.                         bNeedSave = FALSE;
  332.                     }
  333.  
  334.                     line_no = 1;
  335.                     return 0;
  336.  
  337.                 case IDM_SAVE :
  338.                     if (szRealFileName[0])
  339.                     {
  340.                         if (WriteFile(hwnd, hwndEdit, &of, (LPSTR)szRealFileName, FALSE))
  341.                         {
  342.                             bNeedSave = FALSE;
  343.                             return 1;
  344.                         }
  345.                         return 0;
  346.                     }
  347.  
  348.                  /* fall through */
  349.               case IDM_SAVEAS:
  350.                     if (WriteFile (hwnd, hwndEdit, &of, (LPSTR)szFileName, TRUE))
  351.                     {
  352.                         lstrcpy (szRealFileName, szFileName);
  353.                         DoCaption (hwnd, szFileName);
  354.                         bNeedSave = FALSE;
  355.                         return 1;
  356.                     }
  357.                     return 0;
  358.  
  359.                 case IDM_PRINT:
  360.                     PrintFile(hInst, hwnd, hwndEdit, szRealFileName[0] ?
  361.                                          (LPSTR) szRealFileName : szUntitled);
  362.                     return 0;
  363.  
  364.                 case IDM_RUN:
  365.                     if (bIsProcessing)
  366.                     {
  367.                         char szBuffer[80];
  368.                         wsprintf(szBuffer, "Already processing %s ", (LPSTR) szFileNameIn);
  369.                         (void) MessageBox(hwnd, szBuffer, szAppName,
  370.                                     MB_OK | MB_ICONEXCLAMATION);
  371.                     }
  372.  
  373.                     else
  374.                     {
  375.                         if (!bNeedSave || IDCANCEL !=
  376.                                    AskAboutSave (hwnd, szRealFileName))
  377.                         {
  378.                             lstrcpy(szFileNameIn, szRealFileName[0] ? szRealFileName : "lf.in");
  379.                             if (GetOutFile(hwnd))
  380.                             {
  381.                                 bIsProcessing = TRUE;
  382.                                 if (!lf_main(hwnd, szFileNameIn, szFileNameOut, hInst))
  383.                                 {
  384.                                     char szBuffer[80];
  385.                                     int curr_line;
  386.                                     int line_len;
  387.                                     wsprintf(szBuffer,"Could not execute %s ",
  388.                                                    (LPSTR) szFileNameIn);
  389.                                     MessageBox(hwnd, (LPSTR)szBuffer, "LF Error", MB_OK | MB_ICONEXCLAMATION);
  390.  
  391.                                     // set the selection to the error line
  392.                                     curr_line = SendMessage(hwndEdit, EM_LINEFROMCHAR, -1, 0L);
  393.                                     SendMessage(hwndEdit, EM_LINESCROLL, 0, MAKELONG((line_no - 1) - curr_line,0));
  394.                                     curr_line = SendMessage(hwndEdit, EM_LINEINDEX, line_no - 1, 0);
  395.                                     line_len = SendMessage(hwndEdit, EM_LINELENGTH, curr_line, 0);
  396.                                     SendMessage(hwndEdit, EM_SETSEL, 0, MAKELONG(curr_line, curr_line + line_len));
  397.                                 }
  398.                             }
  399.                             bIsProcessing = FALSE;
  400.                         }
  401.                     }
  402.                     return 0;
  403.  
  404.                 case IDM_EXIT:
  405.                     SendMessage(hwnd, WM_CLOSE, 0, 0L);
  406.                     return 0;
  407.  
  408.                 case IDM_ABOUT:
  409.                     DialogBox(hInst, "AboutBox", hwnd, lpfnAboutDlgProc);
  410.                     return 0;
  411.  
  412.                 case IDM_UNDO:
  413.                     SendMessage(hwndEdit, WM_UNDO, 0, 0L);
  414.                     return 0;
  415.  
  416.                 case IDM_CUT:
  417.                     SendMessage(hwndEdit, WM_CUT, 0, 0L);
  418.                     return 0;
  419.  
  420.                 case IDM_COPY:
  421.                     SendMessage(hwndEdit, WM_COPY, 0, 0L);
  422.                     return 0;
  423.  
  424.                 case IDM_PASTE:
  425.                     SendMessage(hwndEdit, WM_PASTE, 0, 0L);
  426.                     return 0;
  427.  
  428.                 case IDM_CLEAR:
  429.                     SendMessage(hwndEdit, WM_CLEAR, 0, 0L);
  430.                     return 0;
  431.  
  432.                 case IDM_SELALL:
  433.                     SendMessage(hwndEdit, EM_SETSEL, 0, MAKELONG(0, 32767));
  434.                     return 0;
  435.             }
  436.             break;
  437.  
  438.         case WM_CLOSE :
  439.             if (!bNeedSave || IDCANCEL != AskAboutSave (hwnd, szRealFileName))
  440.       {
  441.                 if (!bIsProcessing || IDCANCEL != AskAboutProcess(hwnd, szFileNameIn))
  442.         {
  443.                     DestroyWindow(hwnd);
  444.                 }
  445.       }
  446.             return 0;
  447.  
  448.         case WM_QUERYENDSESSION :
  449.             if (!bNeedSave || IDCANCEL != AskAboutSave (hwnd, szRealFileName))
  450.       {
  451.                 if (!bIsProcessing || IDCANCEL != AskAboutProcess(hwnd, szFileNameIn))
  452.         {
  453.                     return 1L;
  454.                 }
  455.       }
  456.             return 0;
  457.  
  458.         case WM_DESTROY:
  459.             atree_quit();
  460.             lf_quit = TRUE;
  461.             FreeProcInstance(lpfnAboutDlgProc);
  462.             PostQuitMessage(0);
  463.             return  0;
  464.  
  465.     }
  466.     return DefWindowProc(hwnd, message, wParam, lParam);
  467. }
  468.